﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace workstation.utils
{
    public class ConvertStr
    {
        public static bool ConvertJsonString(string str, ref string returnstr)
        {
            try
            {

                //格式化json字符串
                JsonSerializer serializer = new JsonSerializer();
                TextReader tr = new StringReader(str);
                JsonTextReader jtr = new JsonTextReader(tr);
                object obj = serializer.Deserialize(jtr);
                if (obj != null)
                {
                    StringWriter textWriter = new StringWriter();
                    JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                    {
                        Formatting = Newtonsoft.Json.Formatting.Indented,
                        Indentation = 4,
                        IndentChar = ' '
                    };
                    serializer.Serialize(jsonWriter, obj);
                    returnstr = textWriter.ToString();
                    return true;
                }
                else
                {
                    returnstr = "";
                    return false;
                }
            }
            catch (Exception ex)
            {
                returnstr = "";
                return false;
            }

        }
        public static bool ConvertXMLString(string XMLstring, ref string returnstr)
        {
            try
            {


                //校验是否是XML报文
                if (!XMLstring.Contains("<?xml version"))
                {
                    returnstr = "";
                    return false;
                }
                XmlDocument xmlDocument = GetXmlDocument(XMLstring);
                returnstr = ConvertXmlDocumentTostring(xmlDocument);
                return true;

            }
            catch (Exception ex)
            {
                returnstr = "";
                return false;
            }

        }

        public static string ConvertXmlDocumentTostring(XmlDocument xmlDocument)
        {
            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(memoryStream, null)
            {
                Formatting = System.Xml.Formatting.Indented//缩进
            };
            xmlDocument.Save(writer);
            StreamReader streamReader = new StreamReader(memoryStream);
            memoryStream.Position = 0;
            string xmlString = streamReader.ReadToEnd();
            streamReader.Close();
            memoryStream.Close();
            return xmlString;
        }
        public static XmlDocument GetXmlDocument(string xmlString)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(xmlString);
            return document;
        }


        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }
    }
}
